package net.gz01.wallet.common;

import java.util.Random;
import java.util.UUID;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;

import net.gz01.util.ZODate;
import net.gz01.util.ZOIDWorker;
import net.gz01.util.ZOString;

/**
 * ID生成器
 * 
 * @date 2015年11月2日
 */
public class ZOIDGenerate {
	
	private static final ZOIDWorker worker = new ZOIDWorker(10);
	
	// 纯数字验证码
	public static final String VERIFY_CODES_INT = "0123456789";
	public static final String ORG_PREFIX = "6";
	public static final String MERCH_PREFIX_PERSONAL = "1";
	public static final String MERCH_PREFIX_COM = "2";

	/**
	 * 生成32位UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "").toLowerCase();
	}

	/**
	 * 下一个订单号
	 * 
	 * @return
	 */
	public static String nextOrderNo() {
		return "F" + String.valueOf(worker.nextId());
	}

	/**
	 * 下一个分润订单号
	 * 
	 * @return
	 */
	public static String nextShareTradeNo() {
		return "FS" + String.valueOf(worker.nextId());// 所有订单前面都加字符串s
	}

	/**
	 * 获取流水号
	 * 
	 * @return
	 */
	public static String getSerialNumber() {
		return String.valueOf(worker.nextId());
	}

	/**
	 * 生成纯数字验证码
	 * 
	 * @param length
	 *            生成验证码的长度
	 * @return
	 */
	public static String getIntRandomCode(int length) {
		int codesLen = VERIFY_CODES_INT.length();
		Random rand = new Random(System.currentTimeMillis());
		StringBuilder verifyCode = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			verifyCode.append(VERIFY_CODES_INT.charAt(rand.nextInt(codesLen - 1)));
		}
		return verifyCode.toString();
	}

	
	public static String getNextMerchCode(boolean isCompany) {
		return getNextMerchCode(isCompany, null);
	}

	public static String getNextMerchCode(boolean isCompany, String dataSourceName) {
		DbPro db;
		if (StrKit.notBlank(dataSourceName)) {
			db = Db.use(dataSourceName);
		} else {
			db = Db.use();
		}
		StringBuilder sb = new StringBuilder();
		String nextMerchCode = db.queryFirst("select nextval('merchCode')").toString();
		if (isCompany) {
			sb.append(MERCH_PREFIX_COM).append(ZOString.appendZeroToLeft(nextMerchCode, 5));
		} else {
			sb.append(MERCH_PREFIX_PERSONAL).append(ZOString.appendZeroToLeft(nextMerchCode, 5));
		}
		
		return sb.toString();
	}

	public static String getNextOrgCode() {
		return getNextOrgCode(null);
	}

	public static String getNextOrgCode(String dataSourceName) {
		DbPro db;
		if (StrKit.notBlank(dataSourceName)) {
			db = Db.use(dataSourceName);
		} else {
			db = Db.use();
		}
		StringBuilder sb = new StringBuilder();
		String nextOrgCode = db.queryFirst("select nextval('orgCode')").toString();
		sb.append(ORG_PREFIX).append(ZOString.appendZeroToLeft(nextOrgCode, 3));
		return sb.toString();
	}

	/**
	 * 生成yyyyMMdd+8位数字的订单号
	 * 
	 * @return
	 */
	public static String getDateOrderNo() {
		String nextOrderNo = Db.queryFirst("select nextval('orderNo')").toString();
		if (nextOrderNo.length() < 8) {
			nextOrderNo = ZOString.appendZeroToLeft(nextOrderNo, 8);
		} else {
			nextOrderNo = nextOrderNo.substring(nextOrderNo.length() - 8);
		}
		StringBuffer orderNo = new StringBuffer();
		orderNo.append(ZODate.getDateNumber());
		orderNo.append(nextOrderNo);
		return orderNo.toString();
	}

	/**
	 * 生成yyyyMMdd+8位数字的订单号
	 * 
	 * @param dataSourceName
	 * @return
	 */
	public static String getDateOrderNo(String dataSourceName) {
		String nextOrderNo = Db.use(dataSourceName).queryFirst("select nextval('orderNo')").toString();
		if (nextOrderNo.length() < 8) {
			nextOrderNo = ZOString.appendZeroToLeft(nextOrderNo, 8);
		} else {
			nextOrderNo = nextOrderNo.substring(nextOrderNo.length() - 8);
		}
		StringBuffer orderNo = new StringBuffer();
		orderNo.append(ZODate.getDateNumber());
		orderNo.append(nextOrderNo);
		return orderNo.toString();
	}

	/**
	 * 6位长度的订单号，如果数据库中orderNo的值小于6位，左边补0到6位长度，如果超出6位，原样返回
	 * 
	 * @return
	 */
	public static String getSixOrderNo() {
		String nextOrderNo = Db.queryFirst("select nextval('orderNo')").toString();
		if (nextOrderNo.length() < 6) {
			nextOrderNo = ZOString.appendZeroToLeft(nextOrderNo, 6);
		}
		return nextOrderNo;
	}
}
